<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="zh-CN">
<head>
    <title>登录注册</title>
    <link rel="stylesheet" href="../bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../css/style.css">


</head>

<body>

<img src="../img/bg.jpg" alt="" style="width: 100%">

<div class="content">

    <div class="form sign-in">
        <h2>欢迎回来</h2>
        <label>
            <span>邮箱/用户名</span>  <span id="usertip" style="color:red;float: right"></span>
            <input type="text" name="userName" id="name" >
        </label>
        <label>
            <span>密码</span>
            <input type="password" name="pwd" id="pwd">
            <input type="radio" name="rem" id="rem" checked value="1" ><p style="color: #909399;font-size: 12px;" >自动登录</p>
        </label>

        <p class="forgot-pass"><a href="javascript:">忘记密码？</a></p>

        <button type="button" class="submit" onclick="login()">登 录</button>
    </div>
    <div class="sub-cont">
        <div class="img">
            <div class="img__text m--up">
                <h2>还未注册？</h2>
                <p>立即注册，发现大量机会！</p>
            </div>
            <div class="img__text m--in">
                <h2>已有帐号？</h2>
                <p>有帐号就登录吧，好久不见了！</p>
            </div>
            <div class="img__btn">
                <span class="m--up">注 册</span>
                <span class="m--in">登 录</span>
            </div>
        </div>
        <div class="form sign-up">
            <h2>立即注册</h2>
            <label>
                <span>用户名</span> <span id="nametip" style="color: red ;float: right"></span>
                <input type="text" id="regName">
            </label>
            <label>
                <span>邮箱</span> <span id="emailtip" style="color: red ;float: right"></span>
                <input type="email" id="regEmail">
            </label>
            <label>
                <span>密码</span> <span id="pwdtip" style="color: red ;float: right"></span>
                <input type="password" id="regPwd">
            </label>

            <label>
                <span>手机号</span> <span id="phonetip" style="color: red ;float: right"></span>
                <input type="phone" id="regPhone">
            </label>
            <label>
                <span>上传头像</span> <span id="phototip" style="color: red ;float: right;display: inline-block"></span>
                <input type="file"  name="upload" id="upload"style="display: none;" >
            </label>
            <button type="button" class="submit" onclick=" pading()">注 册</button>
        </div>
    </div>
</div>

<script src="../js/script.js"></script>

<div class="xl-chrome-ext-bar" id="xl_chrome_ext_{4DB361DE-01F7-4376-B494-639E489D19ED}" style="display: none;">
    <div class="xl-chrome-ext-bar__logo"></div>

    <a id="xl_chrome_ext_download" href="javascript:;" class="xl-chrome-ext-bar__option">下载视频</a>
    <a id="xl_chrome_ext_close" href="javascript:;" class="xl-chrome-ext-bar__close"></a>
</div>

<script src="../jquery/jquery-3.4.1.js"></script>
<script>



    function pading() {
        var userName = $("#regName").val()
        var email = $("#regEmail").val()
        var userPassword = $("#regPwd").val()
        var phone = $("#regPhone").val()
        var regEmail = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$")
        var regPhone = /^1[34578]\d{9}$/;
        var paramters = "userName=" + userName + "&email=" + email + "&userPassword=" + userPassword + "&phone=" + phone

        var flag = -1;



        if (userName == null || userName == "") {
            //用户名为空时
            $("#nametip").html("用户名不能为空")
        } else {
            //用户名不为空时
            $("#nametip").html("")
            //判断邮箱是否为空

            if (email == null || email == "") {
                $("#emailtip").html("邮箱不能为空")
            } else {

                if (!regEmail.test(email)) {
                    $("#emailtip").html("邮箱格式错误")
                } else {
                    $("#emailtip").html("")
                    if (userPassword == null || userPassword == '') {
                        $("#pwdtip").html("密码不能为空")
                    } else {
                        $("#pwdtip").html("")
                        if (phone == null || phone == "") {
                            $("#phonetip").html("手机号不能为空")
                        } else {
                            $("#phonetip").html("")
                            if (!regPhone.test(phone)) {
                                $("#phonetip").html("手机号格式错误")
                            } else {
                                $("#phonetip").html("")

                                flag=1
                            }
                        }
                    }
                }

            }

        }


        if (flag == 1) {
            $.ajax({
                url: "http://localhost:8888/pharmacy/register/userRegister",
                type: "post",
                data: paramters,
                success: function (data) {
                    if (data=="login"){
                        alert("该用户名已存在");
                    location.href="/pharmacy/register/userRegister"
                    }

                    if (data=="success"){
                        alert("注册成功")
                        location.href="http://localhost:8888/pharmacy/home/page"
                    }
                }
            })
        }
    }


    function login() {
        var userName=$("#name").val();
        var userPassword=$("#pwd").val()
        var paramters="userName="+userName+"&userPassword="+userPassword
        var value =$("#rem:checked").val()

        $.ajax({

            url:"http://localhost:8888/pharmacy/index/login",
            type:"post",
            data:paramters,
            datatype:"json",
            success:function (data) {

                if (data=="success"){
                    alert("登录成功")
                    //登录成功跳转到商城首页
                    window.location.href = "http://localhost:8888/pharmacy/home/page"
                }else if (data=="fail"){

                    $("#usertip").html("用户名或密码错误")
                }
            }


        })
    }
</script>
<script>

    var allcookies = document.cookie
    // pwd=...;user=...
    var user_pos = allcookies.indexOf("userName");
    var pwd_pos = allcookies.indexOf("pwd");

    var user;

    if (user_pos != -1) {
        user_pos = user_pos + "userName".length + 1;
        //     从cookie_pos开始找到第一个分号的索引，计算取cookie值得结束索引
        var user_end = allcookies.indexOf(";", user_pos);
        //  如果要获取的元素再cookie字符串的最后，是没有分号的，所以会返回-1
        if (user_end == -1) {
            // 结束位置就是cookie的长度
            user_end = allcookies.length;
        }
        //  得到想要的cookie的值
        user = unescape(allcookies.substring(user_pos, user_end));
        $('input[name="userName"]').val(user)
        console.log(user)
    }

    var pwd
    if (pwd_pos != -1) {
        pwd_pos = pwd_pos + "pwd".length + 1;
        //     从cookie_pos开始找到第一个分号的索引，计算取cookie值得结束索引
        var pwd_end = allcookies.indexOf(";", pwd_pos);
        //  如果要获取的元素再cookie字符串的最后，是没有分号的，所以会返回-1
        if (pwd_end == -1) {
            // 结束位置就是cookie的长度
            pwd_end = allcookies.length;
        }
        //  得到想要的cookie的值
        pwd = unescape(allcookies.substring(pwd_pos, pwd_end));
        $('input[name="pwd"]').val(pwd)
        console.log(pwd)

    }
</script>

</body>
</html>